<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Model\BotsModel;
use App\Service\SteamService;

class CheckBotInv extends Command
{
	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $signature = 'check:botinv {--bot_id=} {--app_id=}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = '检查机器人库存.';

	protected $appId = array(730);

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * 获取机器人库存信息
	 * @param  int $botId 机器人ID
	 * @return array
	 */
	protected function getBotsInvByAll($botId)
	{
		return BotsModel::getBotsInvByAll($botId);
	}

	/**
	 * 修改机器人库存
	 * @param  int $botId 机器人ID
	 * @param  int $dotaInvs dota库存
	 * @param  int $csgoInvs csgo库存
	 * @return bool
	 */
	protected function updateBotsInvByBotId($botId, $csgoInvs = NULL, $dotaInvs = NULL)
	{
		return BotsModel::updateBotsInvByBotId($botId, $csgoInvs, $dotaInvs);
	}

	/**
	 * 获取机器人库存
	 * @param  int $botId 机器人ID
	 * @return array
	 */
	protected function getBotsInvByBotId($botId)
	{
		return BotsModel::getBotsInvByBotId($botId);
	}

	/**
	 * 检查机器人库存
	 * @return bool
	 */
	protected function checkBotInv()
	{
		$bots = $this->getBotsInvByAll($this->botId);
		if (!$bots) {
			echo 'query bots info failed'."\r\n";
			return false;
		}

		foreach ($bots as $bot) {
			foreach ($this->appId as $appId) {
				$this->getBotInv($bot['bot_id'], $bot['steam_id'], $appId);
			}
		}
		
		return true;
	}

	/**
	 * 获取机器人库存
	 * @param  int $botId 机器人ID
	 * @param  int $steamId STEAM ID
	 * @param  int $appId 游戏类型
	 * @return bool
	 */
	protected function getBotInv($botId, $steamId, $appId)
	{
		$count = $this->getBotInvsTotals($steamId, $appId);
		if ($count === false) {
			echo 'query steam invs failed, steam_id:'.$steamId.', appId:'.$appId."\r\n";
			return false;
		}

		$status = false;
		switch ($appId) {
			case 730 :
				$botsInv = $this->getBotsInvByBotId($botId);
				if ($botsInv['csgo_invs'] == $count) {
					$status = true;
					break;
				}
				$status = $this->updateBotsInvByBotId($botId, $count);
				break;
			default :
				echo 'appid not found, appid: '.$appId;
		}

		if (!$status) {
			echo 'update bots invs failed, botid:'.$botId.', appid:'.$appId."\r\n";
			return false;
		}
		echo 'update bots invs success, botid:'.$botId.', appid:'.$appId."\r\n";
		return true;
	}

	/**
	 * 获取机器人库存总数
	 * @param  int $steamId STEAM ID
	 * @param  int $appId 游戏类型
	 * @return int
	 */
	protected function getBotInvsTotals($steamId, $appId)
	{
		$botinv = SteamService::getInstance()->getInvsList($steamId, $appId);
		if (!$botinv) {
			$times = 0;
			while ($times < 3) {
				$botinv = SteamService::getInstance()->getInvsList($steamId, $appId);
				if ($botinv) {
					break;
				}
				$times++;
			}
		}

		if ($botinv && $botinv['success'] == false) {
			return false;
		}

		return $botinv['total_inventory_count'];
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle()
	{
		$this->botId = $this->option('bot_id');
		if ($appId = $this->option('app_id')) {
			$this->appId = (array) $appId;
		}

		$this->checkBotInv();
		echo '数据查询完毕!'."\r\n";
		return true;
	}
}